﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.Profile;
using Microsoft.Practices.Unity;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Vit.Web.Models;
using Vit.Common.Extensions;

namespace Vit.Web.Services
{
    public class AccountService : IAccountService
    {
        private IUnityContainer unity;
        private IEnumerable<IAccountProvider> providers;

        public AccountService(IUnityContainer unity)
        {
            this.unity = unity;
        }

        public IEnumerable<IAccountProvider> Providers
        {
            get
            {
                if (providers == null || providers.Count() <= 0)
                {
                    providers = unity.ResolveAll<IAccountProvider>();
                }
                return providers;
            }
        }

        #region IAccountService Members

        public User GetCurrentUser()
        {
            User user = null;
            Providers.ForEach((a, i) => { user = a.GetCurrentUser(); return user == null; });
            return user;
        }

        public User CreateAnoymousUser(string siteName)
        {
            User user = null;
            Providers.ForEach((a, i) => { user = a.CreateAnoymousUser(siteName); return user == null; });
            return user;
        }

        public User GetUserByName(string siteName, string name)
        {
            User user = null;
            Providers.ForEach((a, i) => { user = a.GetUserByName(siteName, name); return user == null; });
            return user;
        }

        public User GetUserById(string siteName, object id)
        {
            User user = null;
            Providers.ForEach((a, i) => { user = a.GetUserById(siteName, id); return user == null; });
            return user;
        }

        public bool CheckUserPermission(User user, string siteName, string area, string controller, string action)
        {
            bool hasPermission = false;
            Providers.ForEach((a, i) => { hasPermission = a.CheckUserPermission(user, siteName, area, controller, action); return hasPermission == false; });
            return hasPermission;
        }

        public bool CheckUserPermission(User user, string siteName, string uri)
        {
            bool hasPermission = false;
            Providers.ForEach((a, i) => { hasPermission = a.CheckUserPermission(user, siteName, uri); return hasPermission == false; });
            return hasPermission;
        }

        public bool CheckUserModelPermission(User user, string siteName, Type entityType, string propertyName)
        {
            bool hasPermission = false;
            Providers.ForEach((a, i) => { hasPermission = a.CheckUserModelPermission(user, siteName, entityType, propertyName); return hasPermission == false; });
            return hasPermission;
        }

        #endregion
    }
}
